package com.ptit.piclient.tools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.ptit.piclient.model.Song;

public class Utils {
	private static Utils instance = null;
	public static String SETTINGS_FILE_NAME = "settings.dat";

	private Utils() {
	}
	public static String getFormatedDuration(int duration){
		String ret = "";
		ret += (duration / 60);
		ret += ":";
		int du = duration % 60;
		if(du < 10) ret += "0";
		ret += du;
		return ret;		
	}
	public static Utils getInstance() {
		if (instance == null)
			instance = new Utils();
		return instance;
	}

	/**
	 * Method to convert song object into a byte array
	 * 
	 */
	public static byte[] serialize(Song obj) {
		ByteArrayOutputStream out = null;
		try {
			out = new ByteArrayOutputStream();
			ObjectOutputStream os = new ObjectOutputStream(out);
			os.writeObject(obj);
			os.close();
		} catch (Exception e) {
		}
		return out.toByteArray();
	}
	/**
	 * Retrieve song object from byte array
	 */
	public static Song deserialize(byte[] data) throws IOException,
			ClassNotFoundException {
		ByteArrayInputStream in = new ByteArrayInputStream(data);
		ObjectInputStream is = new ObjectInputStream(in);
		Song s = (Song) is.readObject();
		is.close();
		in.close();
		return s;
	}
}
